﻿using AntDesign.ProLayout;
using Microsoft.AspNetCore.Components.WebAssembly.Hosting;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.Net.Http;
using Volo.Abp;
using Volo.Abp.AspNetCore.Components.Web.Theming.Routing;
using Volo.Abp.AspNetCore.Components.WebAssembly.Theming;
using Volo.Abp.Modularity;
using Volo.Abp.UI.Navigation;
using Xms.Abp.AspNetCore.Components.Web.Theming.AntDesign;

namespace Xms.Abp.AspNetCore.Components.WebAssembly.Theming.AntDesign;

[DependsOn(
    typeof(AbpAspNetCoreComponentsWebAssemblyThemingModule),
    typeof(XmsAbpAspNetCoreComponentsWebThemingAntDesignModule)
    )]
public class XmsAbpAspNetCoreComponentsWebAssemblyThemingAntDesignModule : AbpModule
{
    public override void ConfigureServices(ServiceConfigurationContext context)
    {
        var environment = context.Services.GetSingletonInstance<IWebAssemblyHostEnvironment>();
        var builder = context.Services.GetSingletonInstance<WebAssemblyHostBuilder>();

        ConfigureAuthentication(builder);
        ConfigureHttpClient(context, environment);
        ConfigureAntDesign(context, builder);   //To be added for TF AntDesign Theme
        ConfigureRouter(context);
        ConfigureUI(builder);
        ConfigureMenu(context);
    }

    private void ConfigureRouter(ServiceConfigurationContext context)
    {
        Configure<AbpRouterOptions>(options =>
        {
            options.AppAssembly = typeof(XmsAbpAspNetCoreComponentsWebAssemblyThemingAntDesignModule).Assembly;
        });
    }

    private void ConfigureMenu(ServiceConfigurationContext context)
    {
        Configure<AbpNavigationOptions>(options =>
        {
            options.MenuContributors.Add(new LayoutMenuContributor(context.Services.GetConfiguration()));
        });
    }
    /// <summary>
    /// To be added for TF AntDesign Theme
    /// Config AntDesign and Layout setting
    /// </summary>
    /// <param name="context"></param>
    /// <param name="builder"></param>
    private void ConfigureAntDesign(ServiceConfigurationContext context, WebAssemblyHostBuilder builder)
    {
        context.Services.AddAntDesign();
        context.Services.Configure<ProSettings>(builder.Configuration.GetSection("ProSettings"));
        context.Services.Configure<TFAntDesignSettings>(builder.Configuration.GetSection("TFAntDesignSettings"));
    }

    private static void ConfigureAuthentication(WebAssemblyHostBuilder builder)
    {
        builder.Services.AddOidcAuthentication(options =>
        {
            builder.Configuration.Bind("AuthServer", options.ProviderOptions);
            options.UserOptions.RoleClaim = JwtClaimTypes.Role;
            options.ProviderOptions.DefaultScopes.Add("Layout");
            options.ProviderOptions.DefaultScopes.Add("role");
            options.ProviderOptions.DefaultScopes.Add("email");
            options.ProviderOptions.DefaultScopes.Add("phone");
        });
    }

    private static void ConfigureUI(WebAssemblyHostBuilder builder)
    {
        builder.RootComponents.Add<App>("#ApplicationContainer");
    }

    private static void ConfigureHttpClient(ServiceConfigurationContext context, IWebAssemblyHostEnvironment environment)
    {
        context.Services.AddTransient(sp => new HttpClient
        {
            BaseAddress = new Uri(environment.BaseAddress)
        });
    }
}

